package intellitor.sqlparser.web.ddl.service.impl;

import intellitor.sqlparser.ddl.DDLReader;
import intellitor.sqlparser.ddl.DDLReaderFactory;
import intellitor.sqlparser.ddl.DDLTranslater;
import intellitor.sqlparser.ddl.DDLTranslaterFactory;
import intellitor.sqlparser.ddl.TableBean;
import intellitor.sqlparser.ddl.exception.BaseException;
import intellitor.sqlparser.web.ddl.bean.DdlMainBean;
import intellitor.sqlparser.web.ddl.service.DdlMainService;

import java.util.List;


/**
 * 处理SQL自动化工具的实现类
 * @author DevilSoul
 * @since 2015-10-29
 * @version 1.0.0
 */
public class DdlMainServiceImpl implements DdlMainService {

	@Override
	public void ddlFileUpload(DdlMainBean bean) throws Exception {
		//文件为空不处理
		if(null == bean.getDdlFile()){
			throw new BaseException("上传文件为空！");
		}
		//读取DDL文件
		DDLReader ddlReader = DDLReaderFactory.getDDLReader(DDLReaderFactory.XLSX);
		List<TableBean> list = ddlReader.read(bean.getDdlFile());
		//解析DDL，翻译为SQL
		DDLTranslater trans = DDLTranslaterFactory.getTranslater(DDLTranslaterFactory.ORACLE);
		TableBean tb = new TableBean();
		tb.setTableList(list);
		String sql = trans.translate(tb);
		bean.setSql(sql);

	}

}
